package com.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.activities.Front_items_activity;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.TopPager;
import com.bean.feeds;
import com.dbs.DataHelper;
import com.example.lizhiinformation.R;
import com.me.maxwin.view.XListView;
import com.me.maxwin.view.XListView.IXListViewListener;
import com.myadapter.FragmentAllAdapter;
import com.ownload.Httputil;
import com.ownload.Httputil.OnNetWorkRespones;
import com.tools.ImageLoader;
import com.tools.Json_All;
import com.tools.Path_URL;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class FragmentAll extends Fragment implements IXListViewListener {
	ListView listview;
	int code = 1;
	FragmentAllAdapter adapter;
	private String path1;
	private String path2;
	private String pathtop;
	private String pathitems1;
	private String pathitems2;
	private List<TopPager> topPagers;
	private View topview;

	public FragmentAll(String path1, String path2, String pathtop,
			String pathitems1, String pathitems2) {
		super();
		this.path1 = path1;
		this.path2 = path2;
		this.pathtop = pathtop;
		this.pathitems1 = pathitems1;
		this.pathitems2 = pathitems2;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		return inflater.inflate(R.layout.fragmentall, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		listview = (XListView) view.findViewById(R.id.fragmentall);
		listview.setVisibility(View.GONE);
		topview = getActivity().getLayoutInflater().inflate(
				R.layout.fragmentall_top, null);
		((XListView) listview).setXListViewListener(this);
		((XListView) listview).setPullLoadEnable(true);

		if (new DataHelper(getActivity()).query(path1 + "1" + path2, "带头条") == null) {
			start();
		} else {
			choosevalue();
			frame();
		}
		if (new DataHelper(getActivity()).query(pathtop, "带头部的头") == null) {
			topDown();
		} else {
			topvalue();
		}
	}

	@Override
	public void onRefresh() {
		start();
		onLoad();
	}

	@Override
	public void onLoadMore() {
		code++;
		if (new DataHelper(getActivity()).query(path1 + String.valueOf(code)
				+ path2, "带头条") == null) {
			addlistview(listview, code);
		} else {
			ArrayList<feeds> list1 = new Json_All().Json_All(new DataHelper(
					getActivity()).query(path1 + String.valueOf(code) + path2,
					"带头条"));
			adapter.addData(list1);
		}

		onLoad();

	}

	// listview 加载
	public void start() {

		Httputil.OnWorkDown(path1 + "1" + path2, new OnNetWorkRespones() {

			@Override
			public void OK(String respose) {
				// TODO Auto-generated method stub
				new DataHelper(getActivity()).insert(respose, "带头条", path1
						+ "1" + path2);
				choosevalue();
				frame();
			}

			@Override
			public void ERROR(String error) {
				// TODO Auto-generated method stub

			}
		});

	}

	// listview加载更多数据
	public void addlistview(ListView listview, final int code) {
		Httputil.OnWorkDown(path1 + String.valueOf(code) + path2,
				new OnNetWorkRespones() {

					@Override
					public void OK(String respose) {
						// TODO Auto-generated method stub
						new DataHelper(getActivity()).insert(respose, "带头条",
								path1 + String.valueOf(code) + path2);
						ArrayList<feeds> list1 = new Json_All().Json_All(new DataHelper(
								getActivity()).query(path1 + String.valueOf(code) + path2,
								"带头条"));
						adapter.addData(list1);
					}

					@Override
					public void ERROR(String error) {
						// TODO Auto-generated method stub

					}
				});
	}

	// 停止加载和刷新动画
	private void onLoad() {
		((XListView) listview).stopRefresh();
		((XListView) listview).stopLoadMore();
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日      HH:mm ");
		long millis = System.currentTimeMillis();
		Date date = new Date(millis);
		String format = sdf.format(date);
		((XListView) listview).setRefreshTime(format);

	}

	public void frame() {
		topview.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						Front_items_activity.class);
				intent.putExtra("url", topPagers.get(0).getOid());
				startActivity(intent);
				// listview的点击事件

			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						Front_items_activity.class);
				intent.putExtra("url", adapter.getItem(arg2));
				intent.putExtra("pathitems1", pathitems1);
				intent.putExtra("pathitems1", pathitems2);
				startActivity(intent);
			}
		});

	}

	public void choosevalue() {
		String query = new DataHelper(getActivity()).query(path1 + "1" + path2,
				"带头条");
		ArrayList<feeds> list = new Json_All().Json_All(query);
		adapter = new FragmentAllAdapter(getActivity(), list, listview);
		listview.setAdapter(adapter);
		listview.setVisibility(View.VISIBLE);
	}

	public void topvalue() {
		String query = new DataHelper(getActivity()).query(pathtop, "带头部的头");
		JSONObject object = (JSONObject) JSONObject.parse(query);
		String status = object.getString("status");
		if (status.equals("ok")) {
			JSONObject paramz = object.getJSONObject("paramz");
			JSONArray tops = paramz.getJSONArray("tops");
			topPagers = JSONArray.parseArray(tops.toJSONString(),
					TopPager.class);
			TextView toptextView = (TextView) topview
					.findViewById(R.id.toptextview);
			ImageView topimage = (ImageView) topview
					.findViewById(R.id.topimage);
			toptextView.setText(topPagers.get(0).getSubject());
			ImageLoader.cachr(getActivity(),
					Path_URL.IMAGEURL + topPagers.get(0).getPhoto(), topimage,
					listview);
			listview.addHeaderView(topview);

		}
	}

	public void topDown() {
		Httputil.OnWorkDown(pathtop, new OnNetWorkRespones() {

			@Override
			public void OK(String respose) {
				// TODO Auto-generated method stub
				new DataHelper(getActivity()).insert(respose, "带头部的头", pathtop);
				topvalue();

			}

			@Override
			public void ERROR(String error) {
				// TODO Auto-generated method stub

			}
		});
	}
}
